﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Data.SqlClient;

namespace Common
{
    public class BaseDataAccess
    {

        public const string SqlConnectionString = "Data Source=127.0.0.1;Initial Catalog=AiTools;User ID=sa;Password=123456;Encrypt=False";

        public SqlConnection GetSqlConnection() 
        {
            SqlConnection connection = new SqlConnection(SqlConnectionString);
            try
            {
                connection.Open();
            }
            catch
            {
                throw;
            }
            finally 
            {
                if (connection.State==ConnectionState.Open) connection.Close();
            }
            return connection;
        }

        public DataSet Search(string strSql)
        {
            SqlConnection conn = GetSqlConnection();

            SqlDataAdapter dataAdapter = new SqlDataAdapter(strSql, conn);

            DataSet dataSet = new DataSet();

            dataAdapter.Fill(dataSet);

            return dataSet;
        }

        public DataTable SearchTable(string strSql)
        {
            SqlConnection conn = GetSqlConnection();

            SqlDataAdapter dataAdapter = new SqlDataAdapter(strSql, conn);

            DataSet dataSet = new DataSet();

            dataAdapter.Fill(dataSet);

            return dataSet.Tables[0];
        }

    }
}
